package com.wh.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.legend.security.Authority;
import com.legend.security.PermissionType;
import com.legend.utils.Request;
import com.legend.utils.ReturnResult;
import com.legend.utils.Status;
import com.legend.utils.i18nUtils;
import com.wh.model.WorkTeam;
import com.wh.service.IExt.IWorkTeamService;
import com.wh.service.impl.WorkTeamService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:2014-07-17 13:02:55
*/
@Controller
@RequestMapping(value = "/api/team")
public class WorkTeamController {
    Logger log = Logger.getLogger(WorkTeamController.class);
    private static final String MODULECODE = "WorkTeam";

    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.None)
    @RequestMapping(value = "new", method = RequestMethod.POST)
    @ResponseBody
    public String newTeam(HttpServletRequest request) {
        String data = Request.getData(request);
        ReturnResult result = new ReturnResult();
        if (data != null) {
            WorkTeam team = JSONObject.parseObject(data, WorkTeam.class);
            IWorkTeamService service = WorkTeamService.getInstance();
            team.setTeamFounderId("");
            result = service.insertByModel(team);
        } else {
            result.setStatus(Status.ERROR);
            result.setMsg(i18nUtils.getVal("100007000"));
        }
        return JSONObject.toJSONString(result);
    }
    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.None)
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String updateTeam(HttpServletRequest request) {
        String data = Request.getData(request);
        ReturnResult result = new ReturnResult();
        if (data != null) {
            WorkTeam team = JSONObject.parseObject(data, WorkTeam.class);
            IWorkTeamService service = WorkTeamService.getInstance();
            result = service.editByModel(team);
        } else {
            result.setStatus(Status.ERROR);
            result.setMsg(i18nUtils.getVal("100007000"));
        }
        return JSONObject.toJSONString(result);
    }
    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.None)
    @RequestMapping("{pageIndex}/{pageSize}")
    @ResponseBody
    public String getList(@PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) {
        ReturnResult result = new ReturnResult();
        Map<String,Object> map=new HashMap<String, Object>();
       // map.put("status",1);
        IWorkTeamService service = WorkTeamService.getInstance();
       int count=service.count(map);
        result = service.selectByMap(pageIndex,pageSize,map);
        result.setTotal(count);
        return JSONObject.toJSONString(result);
    }
    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.None)
    @RequestMapping("listAll")
    @ResponseBody
    public String getListAll() {
        ReturnResult result = new ReturnResult();
        Map<String,Object> map=new HashMap<String, Object>();
        // map.put("status",1);
        IWorkTeamService service = WorkTeamService.getInstance();
        int count=service.count(map);
        result = service.selectByMap(map);
        result.setTotal(count);
        return JSONObject.toJSONString(result);
    }
    @Authority(moduleCode = MODULECODE, permissionType = PermissionType.None)
    @RequestMapping("detail/{teamId}")
    @ResponseBody
    public String getTeam(@PathVariable("teamId") String teamId) {
        ReturnResult result = new ReturnResult();
        Map<String,Object> map=new HashMap<String, Object>();
        IWorkTeamService service = WorkTeamService.getInstance();
        int count=service.count(map);
        result = service.selectById(teamId);
        result.setTotal(count);
        return JSONObject.toJSONString(result);
    }
}